﻿<?php
@SESSION_START();

class config
{
    protected static $host = "localhost";
    protected static $username = "root";
    protected static $pass = "";
    protected static $dbname = "shopping365";

    function getURL()
    {
        $pageURL = 'http';
        if (!empty($_SERVER['HTTPS'])) {
            if ($_SERVER['HTTPS'] == 'on') {
                $pageURL .= "s";
            }
        }
        $pageURL .= "://";
        if ($_SERVER["SERVER_PORT"] != "80") {
            $pageURL .= $_SERVER["SERVER_NAME"] . ":" . $_SERVER["SERVER_PORT"] . "/shopping365";
        } else {
            $pageURL .= $_SERVER["SERVER_NAME"];
        }

        return $pageURL . "/";
    }
}

if (!class_exists('query')) {
    class query extends config
    {
        private function connection()
        {
            $connect = mysql_connect(self::$host, self::$username, self::$pass);
            if (!$connect) {
                die("error connect database :" . mysql_error());
            } else {
                mysql_select_db(self::$dbname, $connect);
                mysql_query("SET NAMES 'UTF8'", $connect);
            }

            return $connect;
        }

        function nhomsanpham($tenchuyenmuc)
        {
            $connect = self::connection();
            $query   = mysql_query("select * from $tenchuyenmuc where trangthai = 1 ", $connect);
            $arr;
            $i = 0;
            while ($row = mysql_fetch_array($query)) {
                $arr[$i] = array($row['ma'], $row['nhomsanpham'], $row['trangthai'], $row['muclucID']);
                ++$i;
            }
            mysql_close($connect);
            if ($i > 0) return $arr;
            else return FALSE;
        }

        function shops($chuyemucnhomsp)
        {
            $connect = self::connection();
            $query   = mysql_query("select * from shops where trangthai = 1 and loaisanpham='$chuyemucnhomsp' ", $connect);
            $arr;
            $i = 0;
            while ($row = mysql_fetch_array($query)) {
                $arr[$i] = array($row['mashop'], $row['mataikhoan'], $row['tencuahang'], $row['diachicuahang'], $row['hinhanh']
                , $row['sodienthoai'], $row['email'], $row['website'], $row['khuvuc'], $row['loaisanpham'], $row['trangthai'], $row['mota']);
                ++$i;
            }
            mysql_close($connect);

            return $arr;
        }

        function shop($id)
        {
            $connect = self::connection();
            $query   = mysql_query("select * from shops where trangthai = 1 and mashop = $id ", $connect);
            $arr;
            while ($row = mysql_fetch_array($query)) {
                $arr = array($row['mashop'], $row['mataikhoan'], $row['tencuahang'], $row['diachicuahang'], $row['hinhanh']
                , $row['sodienthoai'], $row['email'], $row['website'], $row['khuvuc'], $row['loaisanpham'], $row['trangthai'], $row['mota']);
            }
            mysql_close($connect);

            #$_SESSION['shop'] = $arr;
            return $arr;
        }

        function shopsOfBoss($id)
        {
            $connect = self::connection();
            $query   = mysql_query("select * from shops where trangthai = 1 and mataikhoan = $id ", $connect);
            $arr;
            $i = 0;
            while ($row = mysql_fetch_array($query)) {
                $arr[$i] = array($row['mashop'], $row['mataikhoan'], $row['tencuahang'], $row['diachicuahang'], $row['hinhanh']
                , $row['sodienthoai'], $row['email'], $row['website'], $row['khuvuc'], $row['loaisanpham'], $row['trangthai'], $row['mota']);
                ++$i;
            }
            mysql_close($connect);

            return $arr;
        }

        function products($loaishop, $shopid)
        {
            $connect = self::connection();
            $query   = mysql_query("select * from $loaishop where trangthai = 2 and mashop = $shopid ", $connect);
            $arr;
            $i = 0;
            while ($row = mysql_fetch_array($query)) {
                $arr[$i] = array($row['ma'], $row['tensanpham'], $row['hinhanh'], $row['nhomsp'], $row['chatlieu'], $row['xuatxu'],
                    $row['giatien'], $row['ngaydangtin'], $row['linkfacebook'], $row['trangthai'], $row['mashop']);
                ++$i;
            }
            mysql_close($connect);

            #$_SESSION['shopProduct'] = $arr;
            return $arr;
        }

        function shopProducts($loaishop, $shopid)
        {
            $connect = self::connection();
            $query   = mysql_query("select * from $loaishop where trangthai > 0 and mashop = $shopid ", $connect);
            $arr;
            $i = 0;
            while ($row = mysql_fetch_array($query)) {
                $arr[$i] = array($row['ma'], $row['tensanpham'], $row['hinhanh'], $row['nhomsp'], $row['chatlieu'], $row['xuatxu'],
                    $row['giatien'], $row['ngaydangtin'], $row['linkfacebook'], $row['trangthai'], $row['mashop']);
                ++$i;
            }
            mysql_close($connect);

            #$_SESSION['shopProduct'] = $arr;
            return $arr;
        }

        function product($tablename, $pid)
        {
            $connect = self::connection();
            $query   = mysql_query("select * from $tablename where trangthai > 0  and ma = $pid ", $connect);
            $arr;
            while ($row = mysql_fetch_array($query)) {
                $arr = array($row['ma'], $row['tensanpham'], $row['hinhanh'], $row['nhomsp'], $row['chatlieu'], $row['xuatxu'],
                    $row['giatien'], $row['ngaydangtin'], $row['linkfacebook'], $row['trangthai'], $row['mashop']);
            }
            mysql_close($connect);

            #$_SESSION['product'] = $arr;
            return $arr;
        }

        function phanloaisanpham($tablename)
        {
            $connect = self::connection();
            $query   = mysql_query("select distinct nhomsp from $tablename where trangthai = 2", $connect);
            $arr;
            $i = 0;
            while ($row = mysql_fetch_array($query)) {
                $arr[$i] = $row['nhomsp'];
                ++$i;
            }
            mysql_close($connect);
            if ($i > 0) return $arr;
            else return FALSE;
        }

        protected function accounts()
        {
            $connect = self::connection();
            $query   = mysql_query("select * from taikhoan where trangthai = 1 ", $connect);
            $arr;
            $i = 0;
            while ($row = mysql_fetch_array($query)) {
                $arr[$i] = array($row['mataikhoan'], $row['hoten'], $row['anhdaidien'], $row['diachifacebook'], $row['email'], $row['sodienthoai'],
                    $row['cmnd'], $row['gioitinh'], $row['namsinh'], $row['noicungu'], $row['tentaikhoan'], $row['matkhau'], $row['ngaythamgia']
                , $row['ngaykyhopdong'], $row['hanhopdong'], $row['iptruycap'], $row['trangthai'],);
                ++$i;
            }
            mysql_close($connect);

            return $arr;
        }

        protected function account($id)
        {
            $connect = self::connection();
            $query   = mysql_query("select * from taikhoan where trangthai = 1 and mataikhoan = $id ", $connect);
            $arr;
            while ($row = mysql_fetch_array($query)) {
                $arr = array($row['mataikhoan'], $row['hoten'], $row['anhdaidien'], $row['diachifacebook'], $row['email'], $row['sodienthoai'],
                    $row['cmnd'], $row['gioitinh'], $row['namsinh'], $row['noicungu'], $row['tentaikhoan'], $row['matkhau'], $row['ngaythamgia']
                , $row['ngaykyhopdong'], $row['hanhopdong'], $row['iptruycap'], $row['trangthai'],);
            }
            mysql_close($connect);

            return $arr;
        }

        function mucluc()
        {
            $connect = self::connection();
            $query   = mysql_query("select * from mucluc where trangthai = 1 ", $connect);
            $arr;
            $i = 0;
            while ($row = mysql_fetch_array($query)) {
                $arr[$i] = array($row['muclucID'], $row['tenmucluc'], $row['trangthai']);
                ++$i;
            }
            $numdata = mysql_num_rows($query);
            mysql_close($connect);

            return $arr;
        }

        function bannerquangcao($tenchuyenmuc)
        {
            $connect = self::connection();
            $query   = mysql_query("select * from quangcao where trangthai = 1 and mucquangcao = '$tenchuyenmuc'", $connect);
            $arr;
            $i = 0;
            while ($row = mysql_fetch_array($query)) {
                $arr[$i] = array($row['maquangcao'], $row['makhachhang'], $row['mucquangcao'], $row['diachiwebsite'], $row['noidungquangcao'],
                    $row['hinhanh'], $row['sodienthoai'], $row['ngaydang'], $row['hanhopdong'], $row['trangthai']);
                ++$i;
            }
            $numdata = mysql_num_rows($query);
            mysql_close($connect);

            return $arr;
        }

        public function insertaccount($arr)
        {
            $connect        = $this->connection();
            $tentaikhoan    = $arr['username'];
            $matkhau        = strrev(sha1($arr['pass']));
            $hoten          = $arr['fullname'];
            $email          = $arr['email'];
            $ipdangki       = $arr['ipaddress'];
            $sodienthoai    = $arr['mobilephone'];
            $gioitinh       = $arr['gender'];
            $namsinh        = $arr['birthyear'];
            $thanhpho       = $arr['city'];
            $loaikhohang    = '';
            $tencuahang     = $arr['shopname'];
            $diachicuahang  = $arr['addressshop'];
            $diachifacebook = $arr['addressfacebook'];
            $now = date('dmY');
            $sql            =
                "INSERT INTO
                    taikhoan(hoten, anhdaidien, diachifacebook, email, sodienthoai, cmnd, gioitinh, namsinh, noicungu, tentaikhoan, matkhau, ngaythamgia, ngaykyhopdong, hanhopdong, iptruycap, trangthai)
                VALUES
                    ('$hoten', '', '$diachifacebook', '$email', '$sodienthoai', '', '$gioitinh', '$namsinh', '$diachicuahang', '$tentaikhoan', '$matkhau', '$now', '$now', '$now', '$ipdangki', 0)";
            $rs             = mysql_query($sql, $connect);
            mysql_close($connect);

            return $rs;
        }
    }
}

if (!class_exists('convert')) {
    class convert
    {
        public function price($p)
        {
            $temp = "";
            for ($i = strlen($p) - 1; $i >= 0; $i--) {
                if ($i % 3 == 0 && $i != strlen($p) - 1) {
                    $temp .= ".";
                }
                $temp .= $p[$i];
            }

            return strrev($temp) . ".000 VNĐ";
        }
    }
}

if (!class_exists('data')) {
    class data extends query
    {
        public function getchuyenmuc($tenchuyenmuc)
        {
            return parent::chuyenmuc($tenchuyenmuc);
        }

        public function getstyleproducts($tenchuyenmuc)
        {
            return parent::styleproducts($tenchuyenmuc);
        }

        public function getproducts($chuyenmuc, $loaisanpham, $shopid)
        {

            return parent::products((trim(strtolower($chuyenmuc))) . (trim(strtolower($loaisanpham))), $id);
        }

        public function getaccounts($id)
        {
            if ($id == 0) return parent::accounts();
            else     return parent::account($id);
        }
    }
}

if (!class_exists('menus')) {
    class menus extends query
    {
        var $menu = NULL;

        #Contructor khoi tao lay du lieu muc luc gan cho bien $menu
        function menus()
        {
            $this->menu = parent::mucluc();
        }
    }
}

if (!class_exists('quangcao')) {
    class quangcao extends query
    {
        var $banner = NULL;

        function quangcao($mucquangcao)
        {
            $this->banner = parent::bannerquangcao($mucquangcao);
        }
    }
}

if (!class_exists('processing')) {
    class processing
    {
        public function checkISQL($str)
        {
            $temp  = strtoupper($str);
            $check = FALSE;
            if (preg_match('/SELECT/', $temp)) {
                $check = TRUE;
            } else if (preg_match('/FROM/', $temp)) {
                $check = TRUE;
            } else if (preg_match('/WHERE/', $temp)) {
                $check = TRUE;
            } else if (preg_match('/UNION/', $temp)) {
                $check = TRUE;
            } else if (preg_match('/DROP/', $temp)) {
                $check = TRUE;
            } else if (preg_match('/TABLE/', $temp)) {
                $check = TRUE;
            } else if (preg_match('/DELETE/', $temp)) {
                $check = TRUE;
            } else if (preg_match('/ORDER BY/', $temp)) {
                $check = TRUE;
            } else if (preg_match('/INSERT/', $temp)) {
                $check = TRUE;
            } else if (preg_match('/OR/', $temp)) {
                $check = TRUE;
            } else if (preg_match('/USERNAME/', $temp)) {
                $check = TRUE;
            } else if (preg_match('/ADMIN/', $temp)) {
                $check = TRUE;
            } else if (preg_match('/PASSWORD/', $temp)) {
                $check = TRUE;
            } else if (preg_match('/AND/', $temp)) {
                $check = TRUE;
            } else if (preg_match('/EXEC/', $temp)) {
                $check = TRUE;
            } else if (preg_match('/LIMIT/', $temp)) {
                $check = TRUE;
            }

            return $check;
        }

        public function nonVitext($str)
        {
            if (!$str) return FALSE;
            $unicode = array(
                'a' => 'á|à|ả|ã|ạ|ă|ắ|ặ|ằ|ẳ|ẵ|â|ấ|ầ|ẩ|ẫ|ậ|Á|À|Ả|Ã|Ạ|Ă|Ắ|Ặ|Ằ|Ẳ|Ẵ|Â|Ấ|Ầ|Ẩ|Ẫ|Ậ',
                'd' => 'đ|Đ',
                'e' => 'é|è|ẻ|ẽ|ẹ|ê|ế|ề|ể|ễ|ệ|É|È|Ẻ|Ẽ|Ẹ|Ê|Ế|Ề|Ể|Ễ|Ệ',
                'i' => 'í|ì|ỉ|ĩ|ị|Í|Ì|Ỉ|Ĩ|Ị',
                'o' => 'ó|ò|ỏ|õ|ọ|ô|ố|ồ|ổ|ỗ|ộ|ơ|ớ|ờ|ở|ỡ|ợ|Ó|Ò|Ỏ|Õ|Ọ|Ô|Ố|Ồ|Ổ|Ỗ|Ộ|Ơ|Ớ|Ờ|Ở|Ỡ|Ợ',
                'u' => 'ú|ù|ủ|ũ|ụ|ư|ứ|ừ|ử|ữ|ự|Ú|Ù|Ủ|Ũ|Ụ|Ư|Ứ|Ừ|Ử|Ữ|Ự',
                'y' => 'ý|ỳ|ỷ|ỹ|ỵ|Ý|Ỳ|Ỷ|Ỹ|Ỵ',
            );
            foreach ($unicode as $nonUnicode => $uni) $str = preg_replace("/($uni)/i", $nonUnicode, $str);

            return $str;
        }
    }
}

if (!class_exists('checks')) {
    class checks
    {
        public static function checkIPClient()
        {
            if ($_SERVER['X_FORWARDED_FOR'] != "") {
                $X_FORWARDED_FOR = explode(",", $_SERVER['X_FORWARDED_FOR']);
                $count           = count($X_FORWARDED_FOR);
                if ($count = 0) {
                    $REMOTE_ADDR = trim($X_FORWARDED_FOR);
                } else {
                    $REMOTE_ADDR = trim($X_FORWARDED_FOR[0]);
                }
            } else {
                $REMOTE_ADDR = $_SERVER['REMOTE_ADDR'];
            }
            if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
                $X_FORWARDED_FOR = explode(",", $_SERVER['HTTP_X_FORWARDED_FOR']);
                $REMOTE_ADDR     = trim($X_FORWARDED_FOR[0]);
            } else {
                $REMOTE_ADDR = $_SERVER['REMOTE_ADDR'];
            }

            return $REMOTE_ADDR;
        }

        public static function is_ajax_request()
        {
            return $_SERVER['HTTP_X_REQUESTED_WITH'] === 'XMLHttpRequest';
        }
    }
}

if (!class_exists('os')) {
    class os
    {
        public static function gzip()
        {
            if (substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip')) ob_start("ob_gzhandler");
            else ob_start();
        }

        public static function sessionLifeTime($nameSession, $time)
        {
            if (isset($_SESSION['LAST_ACTIVITY']) && (time() - $_SESSION['LAST_ACTIVITY'] > $time * 3600)) {
                unset($_SESSION[$nameSession]);
            }
            $_SESSION['LAST_ACTIVITY'] = time();
        }
    }
}

if (!class_exists('convertViText')) {
    class convertViText
    {
        public function getChuyenmucName($nhomsp)
        {
            $nsp = strtolower(trim($nhomsp));
            if ($nsp === 'ttao' || $nsp === 'ttquan' || $nsp === 'ttvay' || $nsp === 'ttkhantat' || $nsp === 'ttdolot' || $nsp === 'ttspkhac') return "THỜI TRANG";
        }

        public function getNhomsp($nhomsp)
        {
            $nsp = strtolower(trim($nhomsp));
            switch ($nsp) {
                case 'ttao' :
                    return "ÁO";
                case 'ttquan' :
                    return "QUẦN";
                case 'ttvay' :
                    return "VÁY";
                case 'ttkhantat' :
                    return "KHĂN & TẤT";
                case 'ttdolot' :
                    return "ĐỒ LÓT";
                case 'ttspkhac' :
                    return "SP KHÁC";
            }
        }

        public function vietHoa($text)
        {
            $t = strtolower(trim($text));
            switch ($t) {
                case 'nam' :
                    return "NAM";
                case 'nu' :
                    return "NỮ";
            }
        }

        public function convert_chuyenmuc($category)
        {
            $n = strtolower(trim($category));
            if ($n == "tt") return "THỜI TRANG";
            else if ($n == "pk") return "PHỤ KIỆN";
            else if ($n == "mvb") return "MẸ & BÉ";
            else if ($n == "ddt") return "ĐỒ ĐIỆN TỬ";
            else if ($n == "dgd") return "ĐỒ GIA DỤNG";
            else if ($n == "bds") return "BẤT ĐỘNG SẢN";
            else if ($n == "dv") return "DỊCH VỤ";
            else return NULL;
        }
        #public function tenchuyenmuc($nsp){
        #	$n = strtoupper($nsp);
        #	if($n =="QUAN" || $n == "Quan") return "QUẦN";
        #	else if($n =="QUẦN" || $n == "Quần") return "QUAN";
        #	else if($n =="AO" ||$n =="Ao") return "ÁO";
        #	else if($n =="ÁO" || $n =="Áo") return "AO";
        #}
        public function chuyennghia($tenchuyenmuc, $pl)
        {
            if ($tenchuyenmuc === "ttao" && $pl === "nam") return "ÁO NAM";
            else if ($tenchuyenmuc === "ttao" && $pl === "nu") return "ÁO NỮ";
            else if ($tenchuyenmuc === "ttquan" && $pl === "nam") return "QUẦN NAM";
            else if ($tenchuyenmuc === "ttquan" && $pl === "nu") return "QUẦN NỮ";
        }

        function nonVitext($str)
        {
            if (!$str) return FALSE;
            $unicode = array(
                'a' => 'á|à|ả|ã|ạ|ă|ắ|ặ|ằ|ẳ|ẵ|â|ấ|ầ|ẩ|ẫ|ậ|Á|À|Ả|Ã|Ạ|Ă|Ắ|Ặ|Ằ|Ẳ|Ẵ|Â|Ấ|Ầ|Ẩ|Ẫ|Ậ',
                'd' => 'đ|Đ',
                'e' => 'é|è|ẻ|ẽ|ẹ|ê|ế|ề|ể|ễ|ệ|É|È|Ẻ|Ẽ|Ẹ|Ê|Ế|Ề|Ể|Ễ|Ệ',
                'i' => 'í|ì|ỉ|ĩ|ị|Í|Ì|Ỉ|Ĩ|Ị',
                'o' => 'ó|ò|ỏ|õ|ọ|ô|ố|ồ|ổ|ỗ|ộ|ơ|ớ|ờ|ở|ỡ|ợ|Ó|Ò|Ỏ|Õ|Ọ|Ô|Ố|Ồ|Ổ|Ỗ|Ộ|Ơ|Ớ|Ờ|Ở|Ỡ|Ợ',
                'u' => 'ú|ù|ủ|ũ|ụ|ư|ứ|ừ|ử|ữ|ự|Ú|Ù|Ủ|Ũ|Ụ|Ư|Ứ|Ừ|Ử|Ữ|Ự',
                'y' => 'ý|ỳ|ỷ|ỹ|ỵ|Ý|Ỳ|Ỷ|Ỹ|Ỵ',
            );
            foreach ($unicode as $nonUnicode => $uni) $str = preg_replace("/($uni)/i", $nonUnicode, $str);

            return $str;
        }

        function stripUnicode($str)
        {
            if (!$str) return FALSE;
            $unicode = array(
                'a' => 'á|à|ả|ã|ạ|ă|ắ|ặ|ằ|ẳ|ẵ|â|ấ|ầ|ẩ|ẫ|ậ|Á|À|Ả|Ã|Ạ|Ă|Ắ|Ặ|Ằ|Ẳ|Ẵ|Â|Ấ|Ầ|Ẩ|Ẫ|Ậ',
                'd' => 'đ|Đ',
                'e' => 'é|è|ẻ|ẽ|ẹ|ê|ế|ề|ể|ễ|ệ|É|È|Ẻ|Ẽ|Ẹ|Ê|Ế|Ề|Ể|Ễ|Ệ',
                'i' => 'í|ì|ỉ|ĩ|ị|Í|Ì|Ỉ|Ĩ|Ị',
                'o' => 'ó|ò|ỏ|õ|ọ|ô|ố|ồ|ổ|ỗ|ộ|ơ|ớ|ờ|ở|ỡ|ợ|Ó|Ò|Ỏ|Õ|Ọ|Ô|Ố|Ồ|Ổ|Ỗ|Ộ|Ơ|Ớ|Ờ|Ở|Ỡ|Ợ',
                'u' => 'ú|ù|ủ|ũ|ụ|ư|ứ|ừ|ử|ữ|ự|Ú|Ù|Ủ|Ũ|Ụ|Ư|Ứ|Ừ|Ử|Ữ|Ự',
                'y' => 'ý|ỳ|ỷ|ỹ|ỵ|Ý|Ỳ|Ỷ|Ỹ|Ỵ',
            );
            foreach ($unicode as $nonUnicode => $uni) $str = preg_replace("/($uni)/i", $nonUnicode, $str);
            $text = strtolower($str);

            return str_replace(" ", "", str_replace("&", "", $text));
        }
    }
}

function debug($value, $label = NULL)
{
    $label = get_tracelog(debug_backtrace(), $label);
    echo getdebug($value, $label);
    exit();
}

function getdebug($value, $label = NULL)
{
    $value = htmlentities(print_r($value, TRUE));

    return "<pre>$label$value</pre>";
}

function get_tracelog($trace, $label = NULL)
{
    $line  = $trace[0]['line'];
    $file  = is_set($trace[1]['file']);
    $func  = $trace[1]['function'];
    $class = is_set($trace[1]['class']);
    $log   = "<span style='color:#FF3300'>-- $file - line:$line - $class-$func()</span><br/>";
    if ($label)
        $log .= "<span style='color:#FF99CC'>$label</span> ";

    return $log;
}

function is_set(&$var, $substitute = null) {
    return isset($var) ? $var : $substitute;
}

?>